package a.client.lib_base.ui.view

import a.client.lib_base.ui.utils.EventBusRegister
import a.client.lib_base.ui.utils.EventBusUtils
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import org.greenrobot.eventbus.EventBus

/**
@author:jacky
@Date:2021/1/18
Activity基类 不使用MVVM模式
 **/
abstract class BaseFrameActivity<VB : ViewBinding, VM : ViewModel>(private val vmClass: Class<VM>) :
    AppCompatActivity() {

    protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this).get(vmClass)
    }

    protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { initViewBinding() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        //ARouter依赖注入
        ARouter.getInstance().inject(this)
        //EventBus注册
        if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) {
            EventBusUtils.register(this)
        }

        initView()
        initViewObserve()
    }

    protected abstract fun initViewBinding(): VB
    protected abstract fun initView()
    protected abstract fun initViewObserve()


    override fun onDestroy() {
        if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) {
            EventBusUtils.unRegister(this)
        }
        super.onDestroy()
    }
}